package com.yq.cp.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class RabbitMqConfig {
    @Resource
    RabbitAdmin rabbitAdmin;


    private static String EXCHANGE_NK="exchange_nk";
    //声明交换机
    @Bean
    public TopicExchange exchange(){
        return new TopicExchange(EXCHANGE_NK,true,false);
    }

    //更新用户信息
    private static String QUEUE_DATA="queue_data";

    @Bean
    public Queue queue(){
        return new Queue(QUEUE_DATA,true,false,false);
    }
    @Bean
    public Binding queueBinding(@Qualifier("queue") Queue queue, @Qualifier("exchange") TopicExchange topicExchange){
        return BindingBuilder.bind(queue).to(topicExchange).with("nk.data.*");
    }

    //创建初始化RabbitAdmin对象
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        // 只有设置为 true，spring 才会加载 RabbitAdmin 这个类
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }
    //创建交换机和对列
    @Bean
    public void createExchangeQueue (){
        rabbitAdmin.declareExchange(exchange());
        rabbitAdmin.declareQueue(queue());
    }
}
